package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;

import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderList;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingListService.class)
@Transactional
public class OrderSettingListServiceImpl implements OrderSettingListService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {


        //获取分页查询条件
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //调用PageHelper的分页方法进行分页
        PageHelper.startPage(currentPage, pageSize);
        Page<OrderList> page = orderDao.findByCondition2(queryString);
        long total = page.getTotal();
        List<OrderList> rows = page.getResult();
        return new PageResult(total, rows);
    }

    @Override
    public void orderConfirm(Integer id) {
        //查询该预约确定预约状态
        Integer count = orderDao.findConfirmStatusById(id);
        if (count > 0) {
            //该预约确定预约状态为'Y'
            return;
        }
        //确定预约，设置确定预约状态为‘Y’
        orderDao.updateOrderConfirm(id);
    }

    @Override
    public void orderCancel(Integer id) {
        //查询该预约的确定预约状态
        Integer count = orderDao.findConfirmStatusById(id);
        if (count <= 0) {
            //该预约确认状态为‘N’
            return;
        }
        //取消预约，设置确定预约状态为‘N’
        orderDao.updateOrderCancel(id);
    }

    @Override
    public Result add(Map map, Integer[] setmealIds) throws Exception {
        //查询当前日期是否开起预约设置
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if (orderSetting == null) {
            //该日期没有进行预约设置，不能预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //查询当日是否预约已满
        Integer number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            //预约人数已满
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //查询当前预约用户是否是会员
        String phoneNumber = (String) map.get("phoneNumber");
        Member member = memberDao.findByTelephone(phoneNumber);
        if (member == null) {
            //该用户不是会员，自动注册为会员
            member = new Member();
            member.setPhoneNumber(phoneNumber);
            member.setRegTime(new Date());
            member.setName((String) map.get("name"));
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            memberDao.add(member);
        }
        //判断用户是否重复预约
        Order order = null;
        Integer memberId = member.getId();//会员id
        for (Integer setmealId : setmealIds) {
            order = new Order();
            order.setSetmealId(setmealId);
            order.setOrderDate(date);
            order.setMemberId(memberId);
        }
        List<Order> orderList = orderDao.findByCondition(order);
        if (orderList != null && orderList.size() > 0) {
            //用户已预约，重复预约
            return new Result(false, MessageConstant.HAS_ORDERED);
        }
        for (Integer setmealId : setmealIds) {
            //添加预约信息
            order = new Order(member.getId(), date, (String) map.get("orderType"), Order.ORDERSTATUS_NO, setmealId);
            orderDao.add(order);
            //修改预约设置的人数信息
            orderSetting.setReservations(reservations + 1);
            orderSettingDao.editReservationsByOrderDate(orderSetting);
        }
        return new Result(true, MessageConstant.ORDER_SUCCESS);
    }
}
